import tkinter as tk
import tkinter.messagebox
import random
import threading
import itertools

root = tk.Tk()
root.title("随机提问")
root.geometry("260x180+400+300")
root.resizable(False, False)

def closeWindow():
    root.destroy()
root.protocol("WM_DELETE_WINDOW", closeWindow)

students = ['dj','hs','kk']
root.flag = False

def update_labels():
    if root.flag:
        lbFirst['text'] = lbSecond['text']
        lbSecond['text'] = lbThird['text']
        lbThird['text'] = next(student_cycle)
        root.after(100, update_labels)  # 100毫秒后再次调用update_labels

def switch():
    root.flag = True
    global student_cycle
    student_cycle = itertools.cycle(random.sample(students, len(students)))
    update_labels()  # 开始更新标签

def btnStartClick():
    t = threading.Thread(target=switch)
    t.start()
    btnStart['state'] = 'disabled'
    btnStop['state'] = 'normal'

def btnStopClick():
    root.flag = False
    tk.messagebox.showinfo('恭喜', '本次中奖：' + lbThird['text'])
    btnStart['state'] = 'normal'
    btnStop['state'] = 'disabled'

btnStart = tk.Button(root, text='开始', command=btnStartClick)
btnStart.place(x=30, y=10, width=80, height=20)

btnStop = tk.Button(root, text='停止', command=btnStopClick)
btnStop['state'] = 'disabled'
btnStop.place(x=150, y=10, width=80, height=20)

lbFirst = tk.Label(root, text='', font=('Arial', 20))
lbFirst.place(x=30, y=50, width=80, height=40)

lbSecond = tk.Label(root, text='', fg='red')
lbSecond.place(x=80, y=10, width=80, height=20)

lbThird = tk.Label(root, text='')
lbThird.place(x=80, y=120, width=100, height=20)

root.mainloop()